﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.ExtensibilityHosting;
using Microsoft.VisualStudio.TeamArchitect.PowerTools;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;
using FeatureBuilder.ContribLib;
using Microsoft.VisualStudio.Shell;

namespace FeatureBuilder.Contrib
{
    [Feature("FeatureBuilder.Contrib")]
    [Export(typeof(IFeatureExtension))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class Feature : BlackboardFeatureExtension<ProcessWorkflow>
    {
        public Feature()
            : base()
        {
        }

        /// <summary>
        /// Import VS Service provider so we can find our package
        /// </summary>
        [Import]
        public SVsServiceProvider ServiceProvider { get; set; }

        /// <summary>
        /// Initialize the Feature Extension instance:
        ///   1. Force our shell package to load to initialize VS launch points
        ///   2. Initialize the Blackboard
        ///   3. Restore the workflow state from the Blackboard
        /// </summary>
        /// <param name="persistedVersion"></param>
        protected override void OnInitialize(Version persistedVersion)
        {
            base.OnInitialize(persistedVersion);

            // Verify the shell package is loaded so that VS launch points 
            // are properly registered.
            var shell = (IVsShell)this.ServiceProvider.GetService(typeof(SVsShell));
            var guid = new Guid(FeaturePackage.Guid);
            IVsPackage package;
            shell.IsPackageLoaded(ref guid, out package);

            if (package == null)
            {
                ErrorHandler.ThrowOnFailure(shell.LoadPackage(ref guid, out package));
            }
        }

        /// <summary>
        /// Extender Packs do not require a solution
        /// </summary>
        public override bool PersistInstanceInSolution
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Extender Packs do not have workflow nor state
        /// </summary>
        public override bool PersistStateInSolution
        {
            get
            {
                return false;
            }
        }
    }
}